﻿
using System.Collections.Generic;
using UnityEngine;

public class TargetAreaManager : MonoBehaviour
{
    public static TargetAreaManager Instance;
    
    public FlowFieldPathfinding flowField;
    private Dictionary<FlowFieldAgent, Vector3> assignedSlots = new Dictionary<FlowFieldAgent, Vector3>();
    private List<Vector3> availableSlots = new List<Vector3>();
    
    private void Awake()
    {
        if (Instance != null)
        {
            Destroy(Instance.gameObject);
        }
        
        Instance = this;
        
        GenerateSlots();
    }
    
    public void Initialize(FlowFieldPathfinding flowField)
    {
        this.flowField = flowField;
        GenerateSlots();
    }
    
    private void GenerateSlots()
    {
        Vector3 targetCenter = flowField.GridToWorldPosition(flowField.Destination);
        
        assignedSlots.Clear();
        availableSlots.Clear();
        
        int ringCount = 10;
        for (int ring = 1; ring <= ringCount; ring++)
        {
            int slotsInRing = Mathf.RoundToInt(2 * Mathf.PI * ring);
            float angleStep = 360f / slotsInRing;
            
            for (int i = 0; i < slotsInRing; i++)
            {
                float angle = i * angleStep * Mathf.Deg2Rad;
                Vector3 slotPos = targetCenter + new Vector3(
                    Mathf.Cos(angle) * ring * flowField.CellSize * 50,
                    0,
                    Mathf.Sin(angle) * ring * flowField.CellSize * 50
                );
                
                availableSlots.Add(slotPos);
            }
        }
    }
    
    public Vector3 AssignSlot(FlowFieldAgent agent)
    {
        if (availableSlots.Count == 0)
        {
            return flowField.GridToWorldPosition(flowField.Destination);
        }
        
        // 找到最近的可用槽位
        Vector3 nearestSlot = availableSlots[0];
        float minDistance = float.MaxValue;
        
        foreach (Vector3 slot in availableSlots)
        {
            float distance = Vector3.Distance(agent.transform.position, slot);
            if (distance < minDistance)
            {
                minDistance = distance;
                nearestSlot = slot;
            }
        }
        
        // 分配槽位
        availableSlots.Remove(nearestSlot);
        assignedSlots.Add(agent, nearestSlot);
        return nearestSlot;
    }
    
    public void ReleaseSlot(FlowFieldAgent agent)
    {
        if (assignedSlots.ContainsKey(agent))
        {
            availableSlots.Add(assignedSlots[agent]);
            assignedSlots.Remove(agent);
        }
    }
    
    public void UpdateSlots()
    {
        // 重新生成槽位
        GenerateSlots();
        
        // 重新分配所有代理的槽位
        List<FlowFieldAgent> agents = new List<FlowFieldAgent>(assignedSlots.Keys);
        foreach (FlowFieldAgent agent in agents)
        {
            ReleaseSlot(agent);
            assignedSlots[agent] = AssignSlot(agent);
        }
    }
    
    private void OnDrawGizmos()
    {
        if (flowField == null || availableSlots == null) return;
        
        // 绘制可用槽位
        Gizmos.color = Color.green;
        foreach (Vector3 slot in availableSlots)
        {
            Gizmos.DrawWireSphere(slot, 0.2f);
        }
        
        // 绘制已分配槽位
        Gizmos.color = Color.magenta;
        foreach (var kvp in assignedSlots)
        {
            Gizmos.DrawSphere(kvp.Value, 0.3f);
        }
    }
}